package com.yy.wga.admin.service.controller;


import com.yy.wga.mp.common.interceptor.RequestUserHolder;
import com.yy.wga.mp.common.utils.date.DateUtils;
import com.yy.wga.mp.common.utils.mapping.BeansUtils;
import com.yy.wga.mp.module.admin.request.AdminBusinessCoursePageRequest;
import com.yy.wga.mp.module.admin.request.AdminBusinessCourseSaveRequest;
import com.yy.wga.mp.module.admin.response.AdminBusinessCoursePageResponse;
import com.yy.wga.mp.module.business.entity.pojo.BusinessCourseDO;
import com.yy.wga.mp.module.business.service.BusinessCourseService;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.Date;

/**
 * 运营后台/商学院课程配置
 *
 * @author guochao
 * @since 2024-10-31 22:12:53
 */
@RestController
@RequestMapping("admin/business/course")
public class AdminBusinessCourseController {
    /**
     * 服务对象
     */
    @Resource
    private BusinessCourseService businessCourseService;

    @PostMapping("page")
    public Result<PageBean<AdminBusinessCoursePageResponse>> page(@Valid @RequestBody AdminBusinessCoursePageRequest request) {
        PageBean<AdminBusinessCoursePageResponse> pageBean = businessCourseService.pageData(request);
        return Result.success(pageBean);
    }

    @PostMapping("save")
    public Result<Boolean> save(@Valid @RequestBody AdminBusinessCourseSaveRequest request) {
        if (request.getStartTime() == null) {
            request.setStartTime(new Date());
        }
        if (request.getEndTime() == null) {
            request.setEndTime(DateUtils.dateAddYears(request.getStartTime(), 100));
        }
        BusinessCourseDO config = BeansUtils.map(request, BusinessCourseDO.class);
        config.setAppKey(RequestUserHolder.getBaseRequest().getAppKey());
        businessCourseService.saveOrUpdate(config);
        return Result.success(Boolean.TRUE);
    }

    @GetMapping("del")
    public Result<Boolean> del(@RequestParam("id") Long id) {
        businessCourseService.removeById(id);
        return Result.success(Boolean.TRUE);
    }
}
